package com.example.administrator.wallet.view.activity.asset;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.AddCurrencyAdapter;
import com.example.administrator.wallet.base.BaseActivity;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.CoinListBean;
import com.example.administrator.wallet.model.bean.CurrencyStatuBean;
import com.example.administrator.wallet.model.bean.InfoUpdateBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class AddCurrencyActivity extends BaseActivity {

    @Bind(R.id.finish_img)
    ImageButton finishImg;
    @Bind(R.id.view_title)
    TextView viewTitle;
    @Bind(R.id.query_icon)
    ImageView queryIcon;
    @Bind(R.id.mList)
    ListView mList;
    @Bind(R.id.activity_add_currency)
    LinearLayout activityAddCurrency;
    @Bind(R.id.loadError)
    TextView loadError;
    private IRequestManager manager;
    private String token;
    private CoinListBean coinListBean;
    private Gson mGson;
    private List<CurrencyStatuBean.DataBean> dataBeen;
    public static AddCurrencyAdapter iAdapter;
    private CurrencyStatuBean currencyStatuBean;
    private InfoUpdateBean infoUpdateBean;
    private int index;
    private int top;

    @Override
    public void init() {
        setStatuColor();
        context = this;

        //初始化一些数据对象，获取本地数据等
        manager = RequestFactory.getReqequestManager();
        token = SharedPrefsUtil.getValue(AddCurrencyActivity.this, "wallet", "token", null);
        currencyStatuBean = new CurrencyStatuBean();
        dataBeen = new ArrayList<>();
        mGson = new Gson();


        initView();
        initData();
    }

    private void initAdapter() {
        iAdapter = new AddCurrencyAdapter(AddCurrencyActivity.this, dataBeen, token);
        mList.setAdapter(iAdapter);

        if (iAdapter.getCount() > 0) {
            loadError.setVisibility(View.GONE);
        }
        mList.setOnScrollListener(new AbsListView.OnScrollListener() {

            /**
             * 滚动状态改变时调用
             */
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                index = mList.getFirstVisiblePosition();
                View v = mList.getChildAt(0);
                top = (v == null) ? 0 : v.getTop();
            }

            /**
             * 滚动时调用
             */
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            }
        });

        iAdapter.position(new AddCurrencyAdapter.OnClickPosition() {
            @Override
            public void getPosition(CurrencyStatuBean.DataBean bean, String statu) {
                manager.httpPost(Api.BaseUrl + Api.currency_statu, Api.setCurrencyStatu(token, bean.getCoin_symbol(), statu), new IRequestCallback() {
                    @Override
                    public void onSuccess(String Response) {
                        infoUpdateBean = mGson.fromJson(Response, InfoUpdateBean.class);
                        RequestCoinList();
                    }

                    @Override
                    public void onFailure(String error) {

                    }
                });
            }
        });
    }

    private void initData() {
        RequestCoinList();
    }


    private void RequestCoinList() {
        manager.httpPost(Api.BaseUrl + Api.get_currency_state, Api.getCurrencyStatu(token), new IRequestCallback() {
            @Override
            public void onSuccess(String Response) {
                currencyStatuBean = mGson.fromJson(Response, CurrencyStatuBean.class);
                if (currencyStatuBean.getCode() == 200) {
                    dataBeen = currencyStatuBean.getData();
                    initAdapter();
                    mList.setSelectionFromTop(index, top);
                }
            }

            @Override
            public void onFailure(String error) {
                Log.e("TAG", error);
            }
        });
    }

    private void initView() {
        finishImg.setVisibility(View.VISIBLE);
        viewTitle.setText(R.string.add_assets);
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_add_currency;
    }


    @OnClick({R.id.finish_img, R.id.query_icon})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.finish_img:
                finish();
                break;
            case R.id.query_icon:
                Intent intent = new Intent(AddCurrencyActivity.this, QueryCoinActivity.class);
                startActivity(intent);
                break;
        }
    }
}
